#pragma once

#include <sys/epoll.h>
// #include "Define.h"

#define MAXEVENTS 128
// #define SERVER_PORT 9898

// class CMemPool;

class CServer
{
public:
    CServer();
    virtual ~CServer();

public:
    virtual bool Init(int ilfd);
    virtual bool Start();
    virtual bool Stop();
    virtual bool Destroy();

    // 获得本服务连接客户端的个数
    virtual unsigned int GetClientCount() = 0;
    // 进程间通讯，向本进程写入数据
    virtual int IpcSend(void *pSrc, int iLen) = 0;

    static int BindAndListen(unsigned short nPort);

protected:
    virtual bool ErrorEvent(epoll_event &ee) = 0;
    virtual bool InputEvent(epoll_event &ee) = 0;
    virtual bool OutputEvent(epoll_event &ee) = 0;

    static bool SetNonBlock(int ifd);
    bool UpdateEvents(int ifd, int iEvnts, int iOpt, void *pData);

private:
    bool EventHandle();

protected:
    int m_ilfd;   // 监听套结字fd
    int m_iefd;   // epoll fd
    int m_irspfd; // socketpair通讯套接字中的读套接字
    int m_iwspfd; // socketpair通讯套接字中的写套接字

private:
    bool m_bRunning;
    epoll_event *m_pEvnts;
};
